package com.example.yyy

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.IBinder
import android.provider.Settings
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_progress.*
import org.jetbrains.anko.intentFor
import org.jetbrains.anko.startService
import org.jetbrains.anko.toast


class ProgressActivity : AppCompatActivity() {

    private var mProgress = 0
    private val handler = Handler()
    private var boolean = 1
    private var intentService:Intent? = null
    private var mBinder: FloatingService.TestBinder? = null
    private var mService: FloatingService? = null
    private var idBundleService: Boolean = false
    private companion object val REQUEST_CODE = 1
    private val myFirstConn = object : ServiceConnection{
        override fun onServiceDisconnected(name: ComponentName?) {
            idBundleService = false
            mBinder = null
            mService = null
        }

        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
            mBinder = service as FloatingService.TestBinder?
            mService = mBinder?.getService()
            idBundleService = true
        }

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_progress)
        SendSimpleNotify(this,"标题","我要发布的内容1")
        SendStyleNotify(this,"标题","我要发布的内容2",1)
        SendRemoteNotify(this,"标题","我要发布的内容")
        //SendStyleNotify(this,"标题","我要发布的内容",1)
        //SendStyleNotify(this,"标题","我要发布的内容",2)
        //设置最大值
        pb_pa_progress.max = 100
        //设置默认进度
        pb_pa_progress.progress = 0
        //设置进度条图形
        pb_pa_progress.progressDrawable = resources.getDrawable(R.drawable.progress)

        btn_pa_count.setOnClickListener {
//            pb_pa_progress.progress = et_pa_count.text.toString().toInt()
//            SendFloatNotify(this,"标题","我要发布的内容3")
            startFloatingService()
//            kotlin.run {
//                Thread.sleep(10000)
//                SendLockNotify(this,"标题","我要发布的内容3",true)
//            }
        }

        pb_pa_progressNew.max = 100
        pb_pa_progressNew.isEnabled = false
        pb_pa_progressNew.progress = 0
        handler.post(animation)
        handler.post(Counter())
    }

    fun startFloatingService(){
        if (if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                !Settings.canDrawOverlays(this)
            } else {
               false
            }
        ){
            startActivityForResult(Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:"+packageName)), REQUEST_CODE);
        }else{
//            startService(Intent(this, FloatingService::class.java)
            //不能停止服务
//            startService<FloatingService>()
            //to传参
//            intentFor<FloatingService>("request_content" to "参数")
            //Pair传参
            if (boolean%2!=0){
                intentService = intentFor<FloatingService>(Pair("request_content" , "参数"))
//                startService(intentService)
                //绑定方式启动
                val bindService = bindService(intentService, myFirstConn, Context.BIND_AUTO_CREATE)
                if (idBundleService) mService?.showFloatingWindow("122",true)
                toast("222222${mService==null}")
                boolean++
            }else{
                if (intentService !=null&&idBundleService) {
//                    stopService(intentService)
                    unbindService(myFirstConn)
                }
                toast("233333")
            }

        }
    }

    inner class Counter: Runnable{
        var counter = 0
        override fun run() {
            SendCounterNotify(this@ProgressActivity,"标题","我要发布的内容4",counter)
            counter++
            if (counter<=100){
                handler.postDelayed(this,500)
            }
        }

    }
    private val animation = object :Runnable{
        override fun run() {
            if(mProgress<=100){
                pb_pa_progressNew.progress = mProgress
                pb_pa_progressNew.text("当前处理进度为：${mProgress}%")
                //刷新
                handler.postDelayed(this,500)
                mProgress++
            }else{
                mProgress = 0
                pb_pa_progressNew.isEnabled = true
            }
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (requestCode==REQUEST_CODE){
            if (if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    !Settings.canDrawOverlays(this)
                } else {
                    true
                }
            ){
                startActivityForResult(Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package: $packageName")), REQUEST_CODE);
            }else{
                startService(Intent(this, FloatingService::class.java))
            }
        }
        super.onActivityResult(requestCode, resultCode, data)
    }
}